﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DatabaseTablesViewModel.cs" company="Akzente.IT">
//   (c) 2012 Akzente.IT
// </copyright>
// <summary>
//   The view model for database.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace AkzenteIT.DatabaseDocumentationTool.ViewModels
{
    using System.Collections.Generic;

    using AkzenteIT.DatabaseDocumentationTool.ViewModels.Base;

    using Microsoft.SqlServer.Management.Smo;

    /// <summary>
    /// The view model for database.
    /// </summary>
    public class DatabaseTablesViewModel : NodeViewModel
    {
        #region Fields

        /// <summary>
        /// The database.
        /// </summary>
        private readonly Database database;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="DatabaseTablesViewModel"/> class.
        /// </summary>
        /// <param name="parent">
        /// The parent.
        /// </param>
        /// <param name="database">
        /// The database.
        /// </param>
        public DatabaseTablesViewModel(DatabaseViewModel parent, Database database)
            : base(parent)
        {
            this.database = database;
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets a value indicating whether is category.
        /// </summary>
        public override bool IsCategory
        {
            get
            {
                return true;
            }
        }

        /// <summary>
        /// Gets the name.
        /// </summary>
        public override string Name
        {
            get
            {
                return "Tables";
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The load children.
        /// </summary>
        /// <returns>
        /// The System.Collections.Generic.IEnumerable`1[T -&gt; Tools.SqlServerDocGenerator.ViewModels.Base.NodeViewModel].
        /// </returns>
        protected override IEnumerable<NodeViewModel> LoadChildren()
        {
            if (!this.database.IsAccessible)
            {
                yield break;
            }

            // This will cause the ExtendedProperties to be prefetched, avoiding unecessary queries
            ScriptingOptions scriptingOptions = new ScriptingOptions();

            scriptingOptions.ExtendedProperties = true;
            this.database.PrefetchObjects(typeof(Table), scriptingOptions);

            foreach (Table table in this.database.Tables)
            {
                yield return new TableViewModel(this, table);
            }
        }

        #endregion
    }
}